package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-04-11
 * Time: 10:42
 */
public class Demo7 {
    public static void main(String[] args) {
        //1.创建一个线程,并没有执行,此时执行的只有main线程
        Thread thread = new Thread(() -> {
          for (int i = 0; i < 10; i++) {
            try {
                   System.out.println(Thread.currentThread().getName() + ": 我还活着");
                   Thread.sleep(1 * 1000);
            } catch (InterruptedException e) {
                   e.printStackTrace();
            }
          }
           System.out.println(Thread.currentThread().getName() + ": 我即将死去");
        });
        //main线程执行,此时的当前进程是main
        System.out.println(Thread.currentThread().getName()+ ": ID: " + thread.getId());
        System.out.println(Thread.currentThread().getName()+ ": 名称: " + thread.getName());
        System.out.println(Thread.currentThread().getName()+ ": 状态: " + thread.getState());
        System.out.println(Thread.currentThread().getName()+ ": 优先级: " + thread.getPriority());
        System.out.println(Thread.currentThread().getName()+ ": 后台线程: " + thread.isDaemon());
        System.out.println(Thread.currentThread().getName()+ ": 活着: " + thread.isAlive());
        System.out.println(Thread.currentThread().getName()+ ": 被中断: " + thread.isInterrupted());
        //新建线程开始执行,此时的当前进程是Thread
        thread.start();

        while (thread.isAlive()) {}
        System.out.println(Thread.currentThread().getName()+ ": 状态: " + thread.getState());
   }
}
